package com.mybatis.generate.provider.db.model;

import com.mybatis.generate.util.DatabaseDataTypesUtils;
import com.mybatis.generate.util.StringHelper;
import com.mybatis.generate.util.JdbcType;

public class Column {
	/**
	 * Reference to the containing table
	 */
	private final Table _table;

	/**
	 * The java.sql.Types type
	 */
	private final int _sqlType;

	/**
	 * The sql typename. provided by JDBC driver
	 */
	private final String _sqlTypeName;

	/**
	 * The name of the column
	 */
	private final String _sqlName;

	/**
	 * True if the column is a primary key
	 */
	private boolean _isPk;

	/**
	 * True if the column is a foreign key
	 */
	private boolean _isFk;

	/**
	 * @todo-javadoc Describe the column
	 */
	private final int _size;

	/**
	 * @todo-javadoc Describe the column
	 */
	private final int _decimalDigits;

	/**
	 * True if the column is nullable
	 */
	private final boolean _isNullable;

	/**
	 * True if the column is indexed(索引)
	 */
	private final boolean _isIndexed;

	/**
	 * True if the column is unique（唯一）
	 */
	private final boolean _isUnique;

	/**
	 * Null if the DB reports no default value
	 */
	private final String _defaultValue;
	
	/**
	 * The comments of column
	 */
	private final String _remarks;
	
	/**
	 * Describe what the DbColumn constructor does
	 * 
	 * @param table
	 * @param sqlType
	 * @param sqlTypeName
	 * @param sqlName
	 * @param size
	 * @param decimalDigits
	 * @param isPk
	 * @param isNullable
	 * @param isIndexed
	 * @param defaultValue
	 * @param isUnique
	 */
	public Column(Table table, int sqlType, String sqlTypeName,
			String sqlName, int size, int decimalDigits, boolean isPk,
			boolean isNullable, boolean isIndexed, boolean isUnique,
			String defaultValue,String remarks) {
		_table = table;
		_sqlType = sqlType;
		_sqlName = sqlName;
		_sqlTypeName = sqlTypeName;
		_size = size;
		_decimalDigits = decimalDigits;
		_isPk = isPk;
		_isNullable = isNullable;
		_isIndexed = isIndexed;
		_isUnique = isUnique;
		_defaultValue = defaultValue;
		_remarks = remarks;
	}

	public Table getTable() {
		return _table;
	}

	public int getSqlType() {
		return _sqlType;
	}

	public String getSqlTypeName() {
		return _sqlTypeName;
	}

	public String getSqlName() {
		return _sqlName;
	}

	public boolean isPk() {
		return _isPk;
	}

	public boolean isFk() {
		return _isFk;
	}

	public int getSize() {
		return _size;
	}

	public int getDecimalDigits() {
		return _decimalDigits;
	}

	public boolean isNullable() {
		return _isNullable;
	}

	public boolean isIndexed() {
		return _isIndexed;
	}

	public boolean isUnique() {
		return _isUnique;
	}

	public String getDefaultValue() {
		return _defaultValue;
	}

	public String getRemarks() {
		return _remarks;
	}
	
	public String getColumnName() {
		return StringHelper.uncapitalize(makeAllWordFirstLetterUpperCase());
	}
	
	public String makeAllWordFirstLetterUpperCase() {
		return StringHelper.makeAllWordFirstLetterUpperCase(StringHelper.toUnderscoreName(getSqlName()));
	}
	
	public String getMethod() {
		return "get" + makeAllWordFirstLetterUpperCase() + "()";
	}
	
	public String setMethod() {
		return "set" + makeAllWordFirstLetterUpperCase();
	}
	
	/*public String getColumnNameFirstLower() {
		return StringHelper.uncapitalize(getColumnName());
	}*/
	
	public String getColumnNameLowerCase() {
		return getColumnName().toLowerCase();
	}
	
	public String getJavaType() {
		return DatabaseDataTypesUtils.getPreferredJavaType(getSqlType(), getSize(), getDecimalDigits());
	}
	
	public String getJdbcType() {
		String result = JdbcType.getJdbcSqlTypeName(getSqlType());
		//if(result == null) throw new RuntimeException("jdbcSqlTypeName is null column:"+getSqlName()+" sqlType:"+getSqlType());
		return result;
	}
}
